package com.slipper.system.role.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.constants.Constants;
import com.slipper.system.role.bo.RolePrivilegeBO;
import com.slipper.system.role.dao.RolePrivilegeDao;
import com.slipper.util.Tool;
import com.slipper.system.role.dao.RoleDao;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import com.slipper.system.role.bo.RoleBO;
import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

import java.util.*;

@Service
@ReadTransactional
public class RoleService {
    @Resource
    private RoleDao roleDao;
    @Resource
    private RolePrivilegeDao rolePrivilegeDao;
    
    public RoleBO queryBy(RoleBO bo) {
        return roleDao.queryBy(bo);
    }
    
    public PageInfo<RoleBO> queryPage(RoleBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(roleDao.queryList(bo));
    }
    
    @WriteTransactional
    public Integer save(RoleBO bo) {
        if (Tool.isBlank(bo.getAdmin())) {
            bo.setAdmin(Constants.NO);
        }

        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return roleDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return roleDao.insert(bo);
        }
    }
    
    @WriteTransactional
    public Integer delete(RoleBO bo) {
        return roleDao.delete(bo);
    }

    public Boolean isSuperAdmin(String employeeId) {
        RoleBO bo = new RoleBO();
        bo.setEmployeeId(employeeId);
        bo.setAdmin(Constants.YES);
        return this.roleDao.queryAdminCnt(bo) > 0;
    }

    public boolean isUse(String id) {
        return this.roleDao.queryUseCnt(id) > 0;
    }

    public List<RoleBO> queryList(RoleBO bo) {
        return roleDao.queryList(bo);
    }

    public List<RolePrivilegeBO> queryPower(RoleBO bo) {
        return this.rolePrivilegeDao.queryByRoleId(bo.getId());
    }

    @WriteTransactional
    public Integer savePower(RoleBO bo) {
        int result = this.rolePrivilegeDao.deleteByRoleId(bo.getId());
        List<RolePrivilegeBO> dataList = new ArrayList<>();
        if (bo.getPrivilegeIds() != null) {
            for (String item : bo.getPrivilegeIds()) {
                String[] typePrivilege = item.split("--");
                String type = typePrivilege[0];
                dataList.add(getRolePrivilege(bo.getId(), type, typePrivilege[1]));
            }
        }


        if (!dataList.isEmpty()) {
            result += this.rolePrivilegeDao.saveList(filterRepeat(dataList));
        }
        return result;
    }

    private static RolePrivilegeBO getRolePrivilege(String roleId, String type, String menuPrivilege) {
        String menuId;
        String privilegeId;
        if (menuPrivilege.indexOf("@") > 0) {
            String[] menuPrivileges = menuPrivilege.split("@");
            menuId = menuPrivileges[0];
            privilegeId = menuPrivileges[menuPrivileges.length - 1];
        } else {
            menuId = null;
            privilegeId = menuPrivilege;
        }

        RolePrivilegeBO rolePrivilegeBO = new RolePrivilegeBO();
        rolePrivilegeBO.setId(UUID.getUUID());
        rolePrivilegeBO.setRoleId(roleId);
        rolePrivilegeBO.setType(type);
        rolePrivilegeBO.setMenuId(menuId);
        rolePrivilegeBO.setPrivilegeId(privilegeId);
        return rolePrivilegeBO;
    }

    private static List<RolePrivilegeBO> filterRepeat(List<RolePrivilegeBO> dataList) {
        Set<String> set = new HashSet<>();
        dataList.stream().filter(item -> Tool.isNotBlank(item.getMenuId())).forEach(item -> set.add(item.getPrivilegeId()));
        return dataList.stream().filter(item -> Tool.isNotBlank(item.getMenuId()) || !set.contains(item.getPrivilegeId())).toList();
    }
}
